package com.cy.lib.net.webservice;

import com.cy.lib.log.MyLog;
import com.cy.lib.net.HttpRequest;

import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.util.HashMap;

/**
 * Created by CY on 2015/10/14.
 */
public class WebServiceRequest {


    public static HttpRequest.ResultInfo request(String url, HashMap<String, Object> map, Object tag, String desKey) {

        StringBuffer sb = new StringBuffer();

        JSONObject jsonObject = new JSONObject(map);
        String json = jsonObject.toString();
        sb.append("request:url=" + url + "\njson=" + json);
        if (desKey != null) {
            json = DESUtil.encryptAsDoNet(desKey, json);
            sb.append("\ndesJson=" + json);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        MyLog.b("请求前：" + sb.toString());
        sb.setLength(0);
        hashMap.put("Json", json);
        HttpRequest request = new HttpRequest(url, hashMap, tag);
        HttpRequest.ResultInfo info = request.startSync();
        MyLog.b("请求后：ResultInfo=" + info.getResponseResult() + "\n" + info.getResponseMessage() + "\nresponseCode=" + info.getResponseCode() + "\nurl=" + url);
        if (info.getResponseCode() == HttpURLConnection.HTTP_OK) {
            String result = new WebXMLParser().parseXml(info.getResponseResult());
            sb.append("\nresult=" + result);
            result = DESUtil.decryptDoNet(desKey, result);
            sb.append("\ndesResult=" + result);
            info.setResponseResult(result);
        }
        MyLog.b("请求后并解密后：" + sb.toString());
        return info;
    }

}
